package algo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class TraitementFichier {

	public static ArrayList<Integer>[] parser (String inFile){
		try{
			BufferedReader br = new BufferedReader(new FileReader(inFile));

			String temp = br.readLine();
			int cpt = 0;
			ArrayList<Integer> res [] = new ArrayList[Integer.parseInt(temp)];
			
			while ((temp=br.readLine()) != null ){
				String[] tab = temp.split(" ");
				for(int i=0;i<(tab.length-1);i++){
					if(i == 0){
						res[cpt] = new ArrayList<Integer>();
					}
					res[cpt].add(Integer.parseInt(tab[i]) -1 );
				}
				cpt++;
			}
			
			br.close();
			return res;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void reverseParser (ArrayList<Integer> list, String outFile){
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(outFile));
			System.out.println("\nNombre de sommet : " + list.size() + "\nOrdre de passage :");
			int cpt = 0;
			for(int i=0; i<list.size();i++){
				if (i == 0){
					System.out.print(list.get(i)+1);
					bw.write(list.get(i)+1);
				}else{
					if(cpt == 20){
						System.out.println();
						cpt = 0;
					}
					System.out.print(" -> " + (list.get(i)+1));
					bw.write(" " + (list.get(i)+1));
				}
				cpt++;
			}

			System.out.println("\n");
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
